FROM alpine
MAINTAINER stesen <stesen.ma@huawei.com>

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN apk --update add gcc make g++ zlib-dev build-base git wget build-base curl bash m4 python3 ncurses rsync autoconf automake libtool ccache

COPY docker_arm/config.mak /opt/build/config_arm.mak
COPY docker_aarch64/config.mak /opt/build/config_aarch64.mak

RUN mkdir -pv /opt/build/openharmony/third_party && \
			cd /opt/build/openharmony/third_party && \
			git clone --depth 1 --single-branch https://gitee.com/openharmony/third_party_musl musl && \
            mkdir -pv /opt/build/openharmony/kernel/linux && \
			cd /opt/build/openharmony/kernel/linux && \
			git clone --depth 1 --single-branch https://gitee.com/openharmony/kernel_linux_5.10 linux-5.10 && \
            mkdir -pv /opt/tools/scripts && \
            cd /opt/build && \
			git clone https://gitee.com/stesen/musl-cross-make_for_openharmony && \
            cd /opt/build/musl-cross-make_for_openharmony && \
            cp /opt/build/config_arm.mak /opt/build/musl-cross-make_for_openharmony/config.mak && \
            make && \
			make install && \
            sed -i '15i#define FNM_EXTMATCH 0' /opt/tools/arm-linux-musleabi/include/fnmatch.h && \
            cd /opt/build && \
            rm -fr musl-cross-make_for_openharmony && \
			git clone https://gitee.com/stesen/musl-cross-make_for_openharmony && \
            cd /opt/build/musl-cross-make_for_openharmony && \
            cp /opt/build/config_aarch64.mak /opt/build/musl-cross-make_for_openharmony/config.mak && \
			make && \
			make install && \
            cd /opt/build && \
            rm -fr /opt/build/openharmony /opt/build/musl-cross-make_for_openharmony

# add for ccache
# use "-v ${CCACHE_DIR}:/CCACHE" in docker run command
RUN mkdir -pv /CCACHE && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/arm-linux-musleabi-cc && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/arm-linux-musleabi-c++ && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/arm-linux-musleabi-cpp && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/arm-linux-musleabi-g++ && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/arm-linux-musleabi-gcc && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/aarch64-linux-musleabi-cc && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/aarch64-linux-musleabi-c++ && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/aarch64-linux-musleabi-cpp && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/aarch64-linux-musleabi-g++ && \
			ln -sv /usr/bin/ccache /usr/lib/ccache/bin/aarch64-linux-musleabi-gcc

ENV CCACHE_DIR=/CCACHE
ENV PATH="/usr/lib/ccache/bin:/opt/tools/bin:${PATH}"
